package com.du.web.sample.util.water;

import cn.hutool.core.io.FileUtil;
import com.du.web.sample.util.ConvertUtil;
import com.du.web.sample.util.water.wrap.HeaderFooterPolicyWrap;
import com.itextpdf.kernel.pdf.PdfWriter;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.usermodel.CharacterRun;
import org.apache.poi.hwpf.usermodel.Range;
import org.apache.poi.openxml4j.util.ZipSecureFile;
import org.apache.poi.xwpf.model.XWPFHeaderFooterPolicy;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

/**
 * @desc: word添加水印
 * @author: shuangdu
 * @date: 2021/9/16
 * @since: 1.8
 */
@Slf4j
public class WordWaterMaker extends WaterMaker {

    public WordWaterMaker(WaterMakeParam waterMakeParam) {
        super(waterMakeParam);
        this.initWordParam();
    }

    private void initWordParam() {
        waterMakeParam.setExtName(FileUtil.extName(waterMakeParam.getFileName()).toLowerCase());
        // 输出名称
        waterMakeParam.setOutFileName(makeOutFileName());
    }

    @Override
    public void doMake() {
        poiMake();
    }

    @Deprecated
    private void toPdf() {
        HWPFDocument doc = null;
        OutputStream out = null;
        try {
            doc = new HWPFDocument(waterMakeParam.getWaterInputStream());
            String content = ConvertUtil.toHtml(doc, "d:\\");
            Document parse = Jsoup.parse(content);
            content = parse.toString();
            log.info("html：{}", content);

            waterMakeParam.setExtName("pdf");
            // 输出名称
            waterMakeParam.setOutFileName(makeOutFileName());

            ConvertUtil.toPdf(content, "d:\\".concat(waterMakeParam.getOutFileName()));

            log.info("输出文件：{}", waterMakeParam.getOutFileName());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (doc != null) {
                try {
                    doc.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    private void docMake() {
        HWPFDocument doc = null;
        OutputStream out = null;
        try {
            ZipSecureFile.setMinInflateRatio(-1.0d);
            doc = new HWPFDocument(waterMakeParam.getWaterInputStream());

//            Range headRange = doc.getOverallRange();
//            CharacterRun characterRun = headRange.insertBefore(waterMakeParam.getWaterContent());
//            characterRun.setBold(true);
//            characterRun.setFontSize(2 * 18);
//
//            // 添加水印
//
//            out = new FileOutputStream("d:\\".concat(waterMakeParam.getOutFileName()));
//            doc.write(out);


            log.info("输出文件：{}", waterMakeParam.getOutFileName());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (doc != null) {
                try {
                    doc.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    private void poiMake() {
        XWPFDocument doc = null;
        OutputStream out = null;
        try {
            ZipSecureFile.setMinInflateRatio(-1.0d);
            doc = new XWPFDocument(waterMakeParam.getWaterInputStream());

            XWPFHeaderFooterPolicy headerFooterPolicy = doc.getHeaderFooterPolicy();
            HeaderFooterPolicyWrap policyWrap = new HeaderFooterPolicyWrap(doc, headerFooterPolicy);
            // 添加水印
            policyWrap.createWatermark(waterMakeParam.getWaterContent());

            out = new FileOutputStream("d:\\".concat(waterMakeParam.getOutFileName()));
            doc.write(out);


            log.info("输出文件：{}", waterMakeParam.getOutFileName());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (doc != null) {
                try {
                    doc.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
